<template :model="materiel">
  <a-drawer title="产品规格维护" width="70%" :bordered="false" placement="right" :closable="false" :visible="visible" @close="onClose">
    <div class="table-page-search-wrapper">
      <a-descriptions bordered>
        <a-descriptions-item label="物料编码" :span="2">
          {{ materiel.Code }}
        </a-descriptions-item>
        <a-descriptions-item label="物料名称" :span="2">
          {{ materiel.Name }}
        </a-descriptions-item>
        <a-descriptions-item label="物料单位" :span="2">
          <EnumName code="Unit" v-model="materiel.Unit"></EnumName>
        </a-descriptions-item>
        <a-descriptions-item label="物料类型" :span="2">
          <CommonSelect v-model="materiel.TypeId" table="MaterielType" disabled></CommonSelect>
        </a-descriptions-item>
        <a-descriptions-item label="物料全称" :span="3">
          {{ materiel.FullName }}
        </a-descriptions-item>
      </a-descriptions>
    </div>
    <a-card title="物料规格" style="width: 100%">
      <div class="table-operator">
        <a-button type="primary" v-action:Add icon="plus" @click="handleAdd">新建</a-button>
      </div>

      <s-table ref="table" size="default" rowKey="Id" :columns="columns" :data="loadData" :rowSelection="rowSelection" showPagination="auto">
        <span slot="action" slot-scope="text, record">
          <template>
            <a v-action:Update @click="handleEdit(record)">修改</a>
          </template>
        </span>
      </s-table>
    </a-card>
    <EditForm ref="editForm" @Success="()=>{this.$refs.table.refresh()}"></EditForm>
  </a-drawer>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import MainSvc from '@/api/Define/MaterielSpecSvc'
import EditForm from './Edit'
import EnumSelect from '@/components/CF/EnumSelect'
import EnumName from '@/components/CF/EnumName'
import CommonSelect from '@/components/CF/CommonSelect'

const columns = [
  { title: 'MES编码', dataIndex: 'MesCode', sorter: true },
  { title: '规格编码', dataIndex: 'Code', sorter: false },
  { title: '规格名称', dataIndex: 'Name', sorter: false },
  { title: '规格描述', dataIndex: 'Desc', sorter: false },
  { title: '操作', dataIndex: 'action', width: '100px', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    STable,
    MainSvc,
    EnumSelect,
    EnumName,
    EditForm,
    CommonSelect
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: { MaterielId: '', Keyword: '' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var _query = Object.assign({}, { ...this.queryParam })
        for (const key in _query) {
          if (moment.isMoment(_query[key])) {
            _query[key] = _query[key].format('YYYY-MM-DD')
          }
        }
        const requestParameters = Object.assign({ sortField: 'CreateTime', sortOrder: 'Asc', Search: _query }, parameter)
        console.log('loadData request parameters:', requestParameters)
        return MainSvc.GetPage(requestParameters)
      },
      selectedRowKeys: [],
      selectedRows: [],
      materiel: { Id: '' }
    }
  },
  filters: {
  },
  created() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    moment,
    openDrawer(record) {
        this.visible = true
        this.materiel = record
        this.resetSearchForm()
        this.$refs.table.refresh()
    },
    onClose() {
        this.visible = false
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
      this.$refs.editForm.openForm(null, this.materiel.Id, this.materiel.Name, '新增')
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
      this.$refs.editForm.openForm(record.Id, this.materiel.Id, this.materiel.Name, '修改')
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm() {
      this.queryParam = { MaterielId: this.materiel.Id, Keyword: '' }
    },
    handleDelete(rows) {
      var thisObj = this
      var ids = rows.map(value => value.Id)
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            MainSvc.Delete(ids).then(result => {
              resolve()
              if (result.Success) {
                thisObj.$message.success('操作成功!')
                thisObj.$refs.table.refresh()
              } else {
                thisObj.$message.error(result.Msg)
              }
            })
          })
        }
      })
    }
  }
}
</script>
